package com.example.teaculture.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teaculture.R;
import com.example.teaculture.model.Headline;

import java.util.List;

public class HeadlineBaseAdapter extends BaseAdapter {

    private Context context;
    private List<Headline> list;

    public HeadlineBaseAdapter(Context context, List<Headline> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_img = convertView.findViewById(R.id.iv_img);
            viewHolder.tv_title = convertView.findViewById(R.id.tv_title);
            viewHolder.tv_info = convertView.findViewById(R.id.tv_info);
            viewHolder.tv_date = convertView.findViewById(R.id.tv_date);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try{
            Glide.with(context).load(list.get(position).image).into(viewHolder.iv_img);
            viewHolder.tv_info.setText(list.get(position).info);
            viewHolder.tv_title.setText(list.get(position).title);
            viewHolder.tv_date.setText(list.get(position).date);
        }catch (Exception e){

        }

        return convertView;
    }

    public final class ViewHolder {
        public ImageView iv_img;
        public TextView tv_title;
        public TextView tv_info;
        public TextView tv_date;
        public ViewHolder() {
        }
    }

}
